<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY
// |-----------------------------------------------------------------------------------
// | Author: 会吃草的鱼 <hebiduhebi@163.com>
// | Copyright (c) 2013-2015, http://www.gooraye.net. All Rights Reserved.
// |-----------------------------------------------------------------------------------
namespace Admin\Controller;
use Admin\Api\DatatreeApi;
use Shop\Api\CategoryApi;
use Shop\Api\OrdersItemApi;
use Shop\Api\ProductApi;
use Shop\Api\OrdersApi;
use Common\Api\WeixinApi;
use Weixin\Api\WxaccountApi;
use Weixin\Api\WxuserApi;
use Shop\Api\WithdrawApi;
use Admin\Api\UserPictureApi;
use Shop\Api\ProductSkuApi;
class WxWebController extends AdminController{
    protected $wxapi;
    protected function _initialize(){
        parent::_initialize();
        $result = apiCall(WxaccountApi::GET_INFO, array( array('id' => 5)));
        if ($result['status'] && is_array($result['info'])) {
            $this -> wxaccount = $result['info'];
            $this -> wxapi = new WeixinApi($this -> wxaccount['appid'], $this -> wxaccount['appsecret']);
        } else {
            exit("公众号信息获取失败，请重试！");
        }
    }
    /*
     * 提现管理
     * */
    public function apply_money_managre(){
        if(empty(I('nickname'))){
            $map = null;
        }else{
            $map['nickname'] = I('nickname');
        }
        $page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));
        $order = " create_time desc ";
        $applyinfo = apiCall(WithdrawApi::WXUERQUERY,array($map,$page,$order,$map,'itboye_withdraw.id,itboye_wxuser.nickname,itboye_wxuser.openid,itboye_withdraw.money,itboye_withdraw.account_name,itboye_withdraw.create_time,itboye_withdraw.status,itboye_withdraw.cash_account,itboye_withdraw.reason'));//采用连表查询

        if($applyinfo['status']){
            $this->assign('nickname',$map['nickname']);
            $this->assign('show',$applyinfo['info']['show']);
            $this->assign('list',$applyinfo['info']['list']);
            $this->display();
        }else{
            LogRecord('INFO:'.$applyinfo['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
            $this->error(L('UNKNOWN_ERR'));
        }
    }
    /*
     * 审核异步提交
     * */
    public function ajax_reviewed(){
        //true代表审核通过
        //false代表审核失败
        $map['id'] = I('id');
        if(I('true_false') == '1'){
            $save['status'] = 1;
            $reviewedres = apiCall(WithdrawApi::SAVE,array($map,$save));
            $verifyres = "恭喜您的提现审核通过了";
        }elseif(I('true_false') == '2'){
            $save['status'] = 2;
            $save['reason'] = I('reason');
            $reviewedres = apiCall(WithdrawApi::SAVE,array($map,$save));
            $verifyres = I('reason');
        }

        if($reviewedres['status']){
            $withres = apiCall(WithdrawApi::GET_INFO,array($map));
            if($withres['status']){
                $spenddata['touser'] = I('openid');
                $spenddata['template_id'] = "hyWCOcMrfw5g8tjKzYDXctus8_o4HaOlNNKcy_Q9ddU";//信息模板
                $spenddata['url'] = "http://weixin.qq.com/download";
                $spenddata['data']['first']['value'] = $verifyres;
                $spenddata['data']['first']['color'] = "#173177";
                $spenddata['data']['keyword1']['value'] = $withres['info']['bank_branch'];
                $spenddata['data']['keyword1']['color'] = "#173177";
                $spenddata['data']['keyword2']['value'] = $withres['info']['cash_account'];
                $spenddata['data']['keyword2']['color'] = "#173177";
                $spenddata['data']['keyword3']['value'] = $withres['info']['money'];
                $spenddata['data']['keyword3']['color'] = "#173177";
                $spenddata['data']['keyword4']['value'] = date('Y-m-d',time());
                $spenddata['data']['keyword4']['color'] = "#173177";
                if(I('true_false') == '1'){
                    $spenddata['data']['keyword5']['value'] = "审核通过";
                }else{
                    $spenddata['data']['keyword5']['value'] = "审核不通过";
                }
                $spenddata['data']['keyword5']['color'] = "#173177";
                $spendres =  $this -> wxapi-> sendTmplMsgToFans($spenddata);
                var_dump($spendres);
            }

        }



    }

    /*
     * 商品管理
     * */
    public function product_manage(){
        $result  = apiCall(CategoryApi::QUERY_NO_PAGING);
        if($result['status']){
            $this->assign("category_list",$result['info']);
        }
        $name = I("name",'');
        $cate = I("cate",'');

        //分页时带参数get参数
        $params = array(
            'name'=>$name,
        );
        $map = array();
        $map['itboye_product.name'] = array('like', '%'.$name.'%');
        if(!empty($cate)){
            $params['cate'] = $cate;
            $this->assign("cur_cate",$cate);
            $ids = subclass_ids($result['info'],$cate);
            if(count($ids) > 0){
                $idstr = implode(',',$ids).','.$cate;
            }else{
                $idstr = $cate;
            }
            $map['itboye_product.cate_id'] = ['in',$idstr];
        }
        $page = array('curpage' => I('get.p', 0), 'size' => '10');
        $order = "createtime desc";
        //连表查询将发布者的数据查询出来
        $result = apiCall(ProductApi::PRODUCT_WXUSER,array($map,$page,$order,$params,'itboye_wxuser.nickname,itboye_wxuser.openid,itboye_product.id,itboye_product.name,itboye_product.createtime,itboye_product.status'));
        if($result['status']){
            $this->assign('name',$name);
            $this->assign('show',$result['info']['show']);
            $this->assign('list',$result['info']['list']);
            $this->display();
        }else{
            LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
            $this->error(L('UNKNOWN_ERR'));
        }
    }
    /*
     * ajax数据返回查看商品详情
     * */
    public function product_detail(){
        $product['id'] = I('id');
        $product['status'] = array('in','-2,1,0');//表示要调出所有类容包括出售的
        $res = apiCall(ProductApi::ALLQUERY, array($product,'','','','id,uid,name,synopsis,dt_origin_country,postage,img_id,type'));
        foreach($res['info']['list'] as $vo){
            $imgcondition['id'] = $vo['img_id'];
            $img = apiCall(UserPictureApi::GET_INFO, array($imgcondition));
            $skucondition['product_id'] = $vo['id'];
            $productsku = apiCall(ProductSkuApi::GET_INFO, array($skucondition));
            $productimglist[$vo['id']]['id'] = $vo['id'];//商品id
            $productimglist[$vo['id']]['uid'] = $vo['uid'];//发布商品的用户id
            $productimglist[$vo['id']]['name'] = $vo['name'];
            $productimglist[$vo['id']]['synopsis'] = $vo['synopsis'];//介绍
            $productimglist[$vo['id']]['dt_origin_country'] = $vo['dt_origin_country'];//货源
            $productimglist[$vo['id']]['postage'] = $vo['postage'];//邮费
            if(count($productimglist[$vo['id']]['imglist']) < 6){
                $productimglist[$vo['id']]['imglist'][] = $img['info'];//需要判断图片是不是大于6张了
            }
            $productimglist[$vo['id']]['price'] = $productsku['info']['price'];
            $productimglist[$vo['id']]['ori_price'] = $productsku['info']['ori_price'];
            //将发布者的信息查出放入数组
            $Wusermap['id'] = $vo['uid'];
            $productwx = apiCall(WxuserApi::GET_INFO, array($Wusermap));
            $productimglist[$vo['id']]['nickname'] = $productwx['info']['nickname'];
            $productimglist[$vo['id']]['avatar'] = $productwx['info']['avatar'];
        }
        echo json_encode($productimglist[$vo['id']]);
    }

    /*
     * ajax商品下架同时给发布商品的用户发送信息
     * */
    public function ajax_undercarriage(){
        $map['id'] = I('id');
        if(I('hightlow') == 'low'){
            $save['status'] = '-2';//表示商品被下架
            $save['reason'] = I('reason');
            $result = apiCall(WxaccountApi::GET_INFO, array( array('id' => 5)));
            if ($result['status'] && is_array($result['info'])) {
                $this -> wxaccount = $result['info'];
                $this -> wxapi = new WeixinApi($this -> wxaccount['appid'], $this -> wxaccount['appsecret']);
            } else {
                exit("公众号信息获取失败，请重试！");
            }
            //将相应商品信息调出
            $result = apiCall(ProductApi::GET_INFO,array($map));
            if($result['status']){
                $spenddata['touser'] = I('openid');
                $spenddata['template_id'] = "uouvV65Hq4zAIBVjcS1BEFrGM1MeqDI73Yf2Zp66-tI";//信息模板
                $spenddata['url'] = "http://weixin.qq.com/download";
                $spenddata['data']['first']['value'] = "打扰一下";
                $spenddata['data']['first']['color'] = "#173177";
                $spenddata['data']['keyword1']['value'] = $result['info']['name'];
                $spenddata['data']['keyword1']['color'] = "#173177";
                $spenddata['data']['keyword2']['value'] = date('Y-m-d',time());
                $spenddata['data']['keyword2']['color'] = "#173177";
                $spenddata['data']['keyword3']['value'] = I('reason');
                $spenddata['data']['keyword3']['color'] = "#173177";
                $spendres =  $this -> wxapi-> sendTmplMsgToFans($spenddata);
            }
        }else{
            $save['status'] = '1';
        }
        $result = apiCall(ProductApi::SAVE,array($map,$save));
        if($result['status']){
            $res['status'] = true;
            $res['info'] = "操作成功";
            echo (json_encode($res));
        }else{
            $res['status'] = false;
            $res['info'] = "操作失败";
            echo (json_encode($res));
        }
    }

    /*
     * 用户监测
     * */
    public function user_monitoring(){
        $result = apiCall(WxaccountApi::GET_INFO, array( array('id' => 5)));
        if ($result['status'] && is_array($result['info'])) {
            $this -> wxaccount = $result['info'];
            $this -> wxapi = new WeixinApi($this -> wxaccount['appid'], $this -> wxaccount['appsecret']);
        } else {
            exit("公众号信息获取失败，请重试！");
        }
        $begin_date = date('Y-m-d',strtotime('-6days'));
        $start_date = date('Y-m-d',strtotime('-1days'));
        $date = array('begin_date'=>$begin_date,'end_date' => $start_date);
        $userinfo = $this -> wxapi->getusercumulate($date);//获得用户的增长数据

        $time = array_column($userinfo['msg']['list'],'ref_date');
        $data = array_column($userinfo['msg']['list'],'cumulate_user');
        $max = max($data);
        $min = min($data);
        $time = json_encode($time);
        $data = json_encode($data);
        $this->assign('time',$time);
        $this->assign('data',$data);
        $this->assign('max',$max+100);
        $this->assign('min',$min-100);
        $imginfo = $this -> wxapi->getusershare($date);//获得图文分享数据
        $this->display();
    }

    /*
     * 发货提醒AJAX
     * */
    public function send_good_remind(){
        $result = apiCall(WxaccountApi::GET_INFO, array( array('id' => 5)));
        if ($result['status'] && is_array($result['info'])) {
            $this -> wxaccount = $result['info'];
            $this -> wxapi = new WeixinApi($this -> wxaccount['appid'], $this -> wxaccount['appsecret']);
        } else {
            exit("公众号信息获取失败，请重试！");
        }
        $order_code = I('order_code');
        //获得下单用户的信息
        $result = apiCall(WxuserApi::GET_INFO, array( array('order_code' => $order_code)));
        if($result['status']){
            $data['touser'] = $result['info']['openid'];
            $result = apiCall(OrdersItemApi::GET_INFO, array( array('order_code' => $order_code)));
            if($result['status']){
                $data['template_id'] = "ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY";
                $data['url'] = "http://weixin.qq.com/download";
                $data['data']['first']['value'] = "您有新的订单";
                $data['data']['first']['color'] = "#173177";
                $data['data']['keynote1']['value'] = "商品名称：". $result['info']['name'];
                $data['data']['keynote2']['value'] = "价格：".$result['info']['price'];
                $data['data']['keynote2']['color'] = "#173177";
            }

            $res = $this -> wxapi->sendTmplMsgToFans($data);
            if($res['errmsg'] == 'ok'){
                echo true;
            }else{
                echo false;
            }
        }else{
            echo false;
        }

    }
    
}